<%@ page import="rcfirewall.conexaoBD,java.util.Arrays,java.sql.*,javax.sql.*"%>
<%@ page import="java.io.*,java.util.*, javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Softpar Firewall</title>
</head>
<body>
<%
//Fist of all we are going to get the parameters from the form from addRegra.jsp
String direction = request.getParameter("direction");
String source = request.getParameter("source");
String destiny = request.getParameter("destiny");
String srcPort = request.getParameter("srcPort");
String dstPort = request.getParameter("dstPort");
String protocol = request.getParameter("protocol");
String action = request.getParameter("action");
String comment = request.getParameter("comment");

//If the comment is blank we ask for the user to insert something. It just can't be null.
if(comment == null){
	out.println("Por favor insira um comentário sobre a nova regra");
}

//The form from addRegra.jsp just send us the alias for destinations, source, ports, etc. We need to verify the true value 
//in the database. 

conexaoBD bd = new conexaoBD();
ResultSet rs = bd.montaDropDown2("select * from DIRECTION where descricao = \'" + direction + "\'");
if (rs.next() == true){
	direction = rs.getString(1);
}

rs = bd.montaDropDown2("select * from DESTINATION where name = \'" + source + "\'");
if (rs.next() == true){
	source = rs.getString(2);
}

rs = bd.montaDropDown2("select * from DESTINATION where name = \'" + destiny + "\'");
if (rs.next() == true){
	destiny = rs.getString(2);
}

rs = bd.montaDropDown2("select * from PORT_NUMBER where service_name = \'" + srcPort + "\'");
if (rs.next() == true){
	srcPort = rs.getString(1);
}
//here we check if the value is zero and change it to 0:65535. That's because the database doesn't accept 0:65535.
if (srcPort.equals("0")){
	srcPort = "0:65535";
}

rs = bd.montaDropDown2("select * from PORT_NUMBER where service_name = \'" + dstPort + "\'");
if (rs.next() == true){
	dstPort = rs.getString(1);
}
if (dstPort.equals("0")){
	dstPort = "0:65535";
}

rs = bd.montaDropDown2("select * from PROTOCOL where protocol_name = \'" + protocol + "\'");
if (rs.next() == true){
	protocol = rs.getString(1);
}

rs = bd.montaDropDown2("select * from TARGET where descricao = \'" + action + "\'");
if (rs.next() == true){
	action = rs.getString(1);
}

//We just print the Resulting Strings to check the commands. It's just a debug option.
out.println("<br></br>");
out.println("Resultado: ");
out.println("<br></br>");

//Printing the rule that will be added.
String ruleAdd = ("iptables -A " + direction + " -p " + protocol + " -s " + source
		+ " --sport " + srcPort + " -d " + destiny + " --dport " + dstPort + " -m comment --comment \"" + comment + "\" -j " + action);

//Inserting the rule in the database so it creates an unique ID. We are going to use it later.
bd.insertBD("insert INTO RULES (ruleAdd) value ('" + ruleAdd + "')" );
//getting the unique ID from the rule that was added.
String ID =null;
rs = bd.montaDropDown2("select * from RULES where ruleAdd ='" + ruleAdd + "'");
if (rs.next() == true){
	ID = rs.getString(1);
}

//String that contains the command tha will be used to delete the rule.
String ruleDelete = ("iptables -D " + direction + " -p " + protocol + " -s " + source
		+ " --sport " + srcPort + " -d " + destiny + " --dport " + dstPort + " -m comment --comment \"" + comment + ". ID da Regra " + ID + "\" -j " + action);

 

//Now that we have the unique ID we can update each field of the database so we can use them later when the user wants to delete the rule.
bd.insertBD("update RULES SET ruleDelete = '" + ruleDelete + "', direction = '" + direction + "', protocol ='" + protocol + "', source ='" + 
			 source + "', srcPort ='" + srcPort + "', destiny ='" + destiny + "', dstPort ='" + dstPort + "', comment ='" + comment + 
			 "', action ='" + action + "', type = '1'  where id ='"+ ID +"'");

//Debug
out.println("<br></br>");
out.println(ruleAdd);
out.println("<br></br>");
out.println(ruleDelete);


//Executes the command to add the rule based on the information sent from the form addRegra.jsp
try {
		 
	    String[] commands = new String[]{"iptables", "-A", direction, "-p", protocol, "-s", source, "--sport", srcPort, "-d", destiny,
				"--dport", dstPort, "-m", "comment", "--comment", comment +  ". ID da Regra " + ID, "-j", action};
	    
	    commands = new String[]{"iptables", "-A", direction, "-p", protocol, "-s", source, "--sport", srcPort, "-d", destiny,
				"--dport", dstPort, "-m", "comment", "--comment", comment + ". ID da Regra " + ID, "-j", action};
	            
	    Process child = Runtime.getRuntime().exec(commands);
	    
	} catch (IOException e) {
}




out.println("<br></br>");
out.println("A Regra acima foi adicionada com sucesso.");


%>
<br></br>
<FORM NAME="listRules" ACTION="listRules.jsp" METHOD="POST">
<INPUT TYPE="SUBMIT" VALUE="Listar Regras Existentes">
</FORM>
<br></br>
<FORM NAME="home" ACTION="index.jsp" METHOD="POST">
<INPUT TYPE="SUBMIT" VALUE="Pagina Inicial">
</FORM>


</body>
</html>